//
//  TDShoukuanViewController.m
//  TFB
//
//  Created by yull on 15/8/15.
//  Copyright (c) 2015年 TD. All rights reserved.
//

#import "TDShoukuanViewController.h"
#import "TDFukuanViewController.h"

@interface TDShoukuanViewController () <UIActionSheetDelegate>

@property (nonatomic, assign) double money;
@property (nonatomic, assign) long long pointMoney;
@property (nonatomic, assign) BOOL hasPoint;
@property (nonatomic, strong) NSString *category;

@end

@implementation TDShoukuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.category = @"00";
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backItem;
}

- (void)setMoney:(double)money
{
    _money = money;
    
    self.moneyLabel.text = [NSString stringWithFormat:@"%.2f", _money];
}

- (void)setCategory:(NSString *)category
{
    if (_category != category) {
        _category = category;
        
        if ([_category isEqualToString:@"00"]) {
            [self.categoryBtn setTitle:@"百货商品" forState:0];
        } else if ([_category isEqualToString:@"01"]) {
            [self.categoryBtn setTitle:@"航空公司" forState:0];
        } else {
            [self.categoryBtn setTitle:@"大型景区售票" forState:0];
        }
        
    }
}

- (IBAction)onClickCategoryBtn:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"商品分类" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"百货商品", @"航空公司", @"大型景区售票", nil];
    [actionSheet showInView:self.view];
}
- (IBAction)onClickNumberBtn:(UIButton *)sender {
    
    NSString *title = [sender titleForState:0];
    
    NSInteger scale = [title isEqualToString:@"00"] ? 100 : 10;
    
    if (self.hasPoint) {
        self.pointMoney = self.pointMoney * scale + [title doubleValue];
        while (self.pointMoney > 100) {
            self.pointMoney /= 10;
        }
        self.money = [[NSString stringWithFormat:@"%lld.%lld", (long long)self.money, (long long)self.pointMoney] doubleValue];
    } else {
        self.money = self.money * scale + [title doubleValue];
    }
}
- (IBAction)onClickBackBtn:(id)sender {
    
    self.money = 0.0;
    self.pointMoney = 0;
    self.hasPoint = NO;
}
- (IBAction)onClickShoukuanBtn:(id)sender {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [TDHttpEngine requestFor_PARORDER_WithCustId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] prdordType:self.category prdordAmt:[NSString stringWithFormat:@"%.2f", self.money] complete:^(BOOL succeed, NSString *msg, id reqBody) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if (succeed) {
            NSString *url = [reqBody objectForKey:@"url"];
            if ([url isKindOfClass:[NSString class]] && url.length) {
                TDFukuanViewController *controller = [[TDFukuanViewController alloc] init];
                controller.url = url;
                
                controller.hidesBottomBarWhenPushed = YES;
                
                NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
                [vcs removeLastObject];
                [vcs addObject:controller];
                [self.navigationController setViewControllers:vcs animated:YES];
            }
        } else {
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}
- (IBAction)onClickPointBtn:(id)sender {
    
    if (self.hasPoint) {
        return;
    }
    
    self.hasPoint = YES;
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        self.category = @"00";
    } else if (buttonIndex == 1) {
        self.category = @"01";
    } else {
        self.category = @"02";
    }
}

@end
